package com.sddz.pacs.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.entity.SysDict;
import com.sddz.pacs.service.SysDictService;
import com.sddz.pacs.support.ResultConstant;
import com.sddz.pacs.util.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by  lixiaofei on 2018\5\19 0019.
 */

@RestController
@Api(description = "字典相关")
@RequestMapping("/dict")
public class SysDictController {


    @Autowired
    private SysDictService sysDictService;

    @PostMapping(value = "/add")
    @SysLogger(value = "字典添加",type = "基础操作")
    @ApiOperation(value = "添加", notes = "添加字典数据")
    @ApiImplicitParam(name = "SysDict", value = "sysDict Json对象", required = true)
    public JsonResult add(@RequestBody SysDict sysDict) {
        //验证数据的完整性
        ValidatorUtils.validateEntity(sysDict);
        boolean result = sysDictService.insertDict(sysDict);
        return result ? JsonResult.success(result, ResultConstant.ADD_SUCCESS) : JsonResult.failure(ResultConstant.ADD_FAILURE);
    }

    @PostMapping(value = "/edit")
    @SysLogger(value = "字典更新",type = "基础操作")
    @ApiOperation(value = "更新", notes = "更新字典数据")
    @ApiImplicitParam(name = "SysDict", value = "sysDict Json对象", required = true)
    public JsonResult edit(@RequestBody SysDict sysDict) {
        //验证数据的完整性
        ValidatorUtils.validateEntity(sysDict);
        boolean result = sysDictService.updateDict(sysDict);
        return result ? JsonResult.success(result, ResultConstant.UPDATE_SUCCESS) : JsonResult.failure(ResultConstant.UPDATE_FAILURE);
    }

    @GetMapping(value = "/delete/{id}")
    @SysLogger(value = "字典删除",type = "基础操作")
    @ApiOperation(value = "删除字典数据")
    @ApiImplicitParam(name = "id", value = "用户唯一标识", required = true)
    public JsonResult delete(@PathVariable("id") String id) {
        boolean result = sysDictService.deleteDict(id);
        return result ? JsonResult.success(result, ResultConstant.DELETE_SUCCESS) : JsonResult.failure(ResultConstant.DELETE_FAILURE);
    }


    @PostMapping("/list")
    @ApiOperation(value = "获取字典列表")
    @ApiImplicitParam()
    public JsonResult list() {
        List<SysDict> list = sysDictService.getDictList();
        return JsonResult.success(list, ResultConstant.LIST_SUCCESS);
    }

    @GetMapping("/getInfo/{id}")
    @SysLogger(value = "查询字典详细信息",type = "字典管理")
    @ApiOperation(value = "获取字典详细信息")
    @ApiImplicitParam(name = "id", value = "用户唯一标识", required = true)
    public JsonResult getInfo(@PathVariable("id") String id) {
        SysDict sysDict = sysDictService.getDictById(id);
        return JsonResult.success(sysDict, ResultConstant.DETAILS_MESS_SUCCESS );
    }

    /**
     * 查询树形列表
     * @return
     */
    @GetMapping("/getSysDictList")
    public JsonResult getSysDictList(){
        EntityWrapper<SysDict> ew = new EntityWrapper<>();
        ew.eq("PARENT_ID","0");
        List<SysDict> list = sysDictService.selectList(ew);
        return JsonResult.success(sysDictService.getSysDictList(list),ResultConstant.SELECT_SUCCESS);
    }
}